package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.provider.api.erp.entity.SkuInfo;
import com.weixing.mall.provider.api.erp.query.SkuQuery;
import com.weixing.mall.provider.api.erp.vo.ResultSkuInfoVo;
import com.weixing.mall.provider.api.model.query.ErpGoodsQuery;
import com.weixing.mall.provider.api.model.vo.ErpProductVo;
import com.weixing.mall.provider.api.model.vo.dto.DepotRPcVo;
import com.weixing.mall.provider.api.model.vo.dto.ErpSyncDto;
import com.weixing.mall.provider.api.model.vo.dto.RpcProductStockDto;
import com.weixing.mall.provider.api.service.DepotServiceRpc;
import com.weixing.mall.provider.api.service.ProductStockServiceRpc;
import com.weixing.mall.provider.erp.service.ErpService;
import com.weixing.mall.provider.mapper.ProductMapper;
import com.weixing.mall.provider.model.enums.GoodsFromEnum;
import com.weixing.mall.provider.service.IErpGoodsSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ErpGoodsSyncServiceImpl implements IErpGoodsSyncService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private DepotServiceRpc depotServiceRpc;
    @Autowired
    private ErpService erpService;
    @Autowired
    private ProductStockServiceRpc productStockServiceRpc;

    @Async
    @Override
    public synchronized void syncErpGoodsToProductStock() {
        //ERP仓库
        Result<DepotRPcVo> result = depotServiceRpc.getBycode(GlobalConstant.DepotCode.ERP);
        //ERP基础商品信息
        List<ErpProductVo> baseInfoList = productMapper.getErpGoodsAllList(GoodsFromEnum.ERP.getValue());
        if(result.isSuccess()
                &&ObjectUtil.isNotEmpty(result.getData())
                && baseInfoList.size() > 0){
            //RPC调用删除原有ERP本地数据
            productStockServiceRpc.delSyncErpData(result.getData().getId());
            //调用同步方法 起始值这里每次执行1000条数据
            recursiveSync(1,1000,baseInfoList,result.getData().getId());
        }
    }

    //递归执行
    private void recursiveSync(int startPage,int pageSize,List<ErpProductVo> baseInfoList,Long depotId){
        SkuQuery query = new SkuQuery();
        query.setDepotid(GlobalConstant.ERP_DEPOT_ID);
        query.setPageindex(startPage);
        query.setPagesize(pageSize);
        Result<ResultSkuInfoVo> result = erpService.selectDepotSkuStockByPage(query);
        if(result.isSuccess()
                && ObjectUtil.isNotEmpty(result.getData())){

            //封装并同步数据
            loadAndSave(result.getData().getData(),baseInfoList,depotId);
            System.out.println("total数据总长度："+result.getData().getTotal());
            System.out.println("集合数据总长度："+result.getData().getData().size());
            System.out.println("指定数据总长度："+pageSize);
            System.out.println("当前页数："+startPage);
            if(result.getData().getData().size() == pageSize){
                //当查询结果数据长度跟指定长度相等时将进行递归 否则将结束
                recursiveSync(startPage+1, pageSize,baseInfoList,depotId);
            }
        }
    }

    //数据组装并保存仓库
    private void loadAndSave( List<SkuInfo> data,List<ErpProductVo> baseInfoList,Long depotId){
        //同步数据封装
      List<RpcProductStockDto> list = Lists.newArrayList();
      //循环ERP库存数据
      for (SkuInfo s:data){
          if(Integer.valueOf(s.getK_num()) == 0){//没库存跳过本次循环
              continue;
          }
          //循环基础商品信息
          for (ErpProductVo e:baseInfoList){
              if(ObjectUtil.isEmpty(e.getProductSn()) || ObjectUtil.isEmpty(e.getStyleNo())){//空数据跳过本次循环
                  continue;
              }
              //入仓条件判断 库存大于0 productSn和styleNo 相等
              //System.out.println("入仓条件->存储:"+s.getK_num()+"->sku:"+e.getProductSn()+"&"+s.getSku()+"->styleid:"+e.getStyleNo()+"&"+s.getStyleid());
              if(Integer.valueOf(s.getK_num()) > 0
                      && e.getProductSn().equals(s.getSku())
                      && e.getStyleNo().equals(s.getStyleid())){
                  RpcProductStockDto rps = new RpcProductStockDto();
                  rps.setGoodsId(e.getGoodsId());
                  rps.setProductId(e.getProductId());
                  rps.setDepotId(depotId);
                  rps.setStock(Integer.valueOf(s.getK_num()));
                  rps.setEnableStock(Integer.valueOf(s.getP_num()));
                  rps.setProductSkuDesp(e.getProductSkuDesp());
                  rps.setStockPrice(e.getCostPrice());
                  rps.setProductSn(e.getProductSn());
                  rps.setStyleNo(e.getStyleNo());
                  rps.setGoodsSn(e.getGoodsSn());
                  rps.setProductImg(e.getProductImg());
                  list.add(rps);
                  break;
              }
          }
      }
        System.out.println("匹配条数:"+list.size());
      //rpc调用同步库存信息
      if(list.size()>0){
          ErpSyncDto es = new ErpSyncDto();
          es.setDepotId(depotId);
          es.setList(list);
          productStockServiceRpc.erpSynData(es);
      }
    }
}
